﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Web.Model;
using Portal.Utility;
using Portal.Web.Utility;
using Portal.Exam.Model;
using Portal.Exam.Bussiness;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class UpdateYesNo 
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {

                WebSessionUtility.CheckSessionTimeOut();
                string QuestionId = System.Web.HttpContext.Current.Request["QuestionId"];
                QuestionCls QuestionCls = new QuestionBll().CreateModel(SiteId, QuestionId);
                if (QuestionCls == null)
                {
                    OAjaxOut.HtmlContent ="Không tìm thấy. Tham số request không hợp lệ!";
                    return OAjaxOut;
                }


                string[] Answer = new string[]{
                    "Đúng","Sai"
                };
                string[] AnswerCode = new string[]{
                    "Yes","No"
                };


                string SelectAnswerText = "<select id=drpSelectAns>";
                for (int iIndex = 0; iIndex < Answer.Length; iIndex++)
                {
                    if (QuestionCls.CorrectOrder == iIndex)
                    {
                        SelectAnswerText += "<option selected value=\"" + AnswerCode[iIndex] + "\">" + Answer[iIndex] + "</option>";
                    }
                    else
                    {
                        SelectAnswerText += "<option value=\"" + AnswerCode[iIndex] + "\">" + Answer[iIndex] + "</option>";
                    }
                }
                SelectAnswerText += "</select>";

                string MonthiId = (string)WebEnvironments.Request(SiteId, "MonthiId");
                string Type = (string)WebEnvironments.Request(SiteId, "Type");

                string SelectLevel =
                  "<select id=drpSelectLevelQuestion>\r\n" +
                  "   <option value=\"0\">Mức độ dễ</option>\r\n" +
                  "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
                  "   <option value=\"2\">Mức độ khó</option>\r\n" +
                  "</select>\r\n";
                if (QuestionCls.Level == 1)
                {
                    SelectLevel =
                      "<select id=drpSelectLevelQuestion>\r\n" +
                      "   <option value=\"0\">Mức độ dễ</option>\r\n" +
                      "   <option value=\"1\" selected>Mức độ trung bình</option>\r\n" +
                      "   <option value=\"2\">Mức độ khó</option>\r\n" +
                      "</select>\r\n";
                }
                if (QuestionCls.Level == 2)
                {
                    SelectLevel =
                      "<select id=drpSelectLevelQuestion>\r\n" +
                      "   <option value=\"0\">Mức độ dễ</option>\r\n" +
                      "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
                      "   <option value=\"2\" selected>Mức độ khó</option>\r\n" +
                      "</select>\r\n";
                }
                string BackUrl = "";// "ExamQuestion.aspx?GroupQuestionId=" + QuestionCls.GroupQuestionId + "&MonthiId=" + QuestionCls.MonthiCls.MonthiId + "&Type=" + QuestionCls.Type;
            
                string RetHtml =
                    " <script language=javascript> \r\n" +
                    " function ClientSaveUpdateYesNo(QuestionId) \r\n" +
                    " { \r\n" +
                    "     SiteId='" + SiteId + "';\r\n" +
                    "     Subject=document.getElementById(\"txtQuestion\").value; \r\n" +
                    "     Index=document.getElementById(\"drpSelectAns\").selectedIndex; \r\n" +
                    "     Point=document.getElementById(\"txtPoint\").value; \r\n" +
                    "     SecondTime = document.getElementById(\"txtSecondTime\").value; \r\n" +
                    "     IsMemory = document.getElementById(\"chkIsMemory\").checked; \r\n" +
                    "     SecondHidden = document.getElementById(\"txtSecondHidden\").value; \r\n" +
                    "     SelectLevelIndex = document.getElementById(\"drpSelectLevelQuestion\").selectedIndex; \r\n" +
                    "     SelectLevel = document.getElementById(\"drpSelectLevelQuestion\").options[SelectLevelIndex].value; \r\n" +

                    "     QuestionId = Portal.Exam.Render.UpdateYesNo.ServerSideUpdateYesNo(SiteId, QuestionId, Subject, Index, Point, SecondTime, IsMemory, SecondHidden, SelectLevel).value; \r\n" +
                    "     if(QuestionId.indexOf(\"ERROR\")!=-1) \r\n" +
                    "      { \r\n" +
                    "         alert(QuestionId); \r\n" +
                    "         return; \r\n" +
                    "     } \r\n" +
                    "     window.open('" + BackUrl + "','_self'); \r\n" +
                    " } \r\n" +



                    " function ClientAddNewYesNoQuestion(MonthiId,QuestionId)\r\n" +
                    " {\r\n"+
                    "       window.open('AddYesNo.aspx?MonthiId='+MonthiId,'_self');\r\n"+
                    " }\r\n"+

                    " </script>\r\n" +
                    "      <div class=FormSaveUpdate>\r\n" +
                    "      <table>" +
                    "         <tr>" +
                    "              <td class=\"Col1\">Mức độ</td>" +
                    "              <td class=\"Col2\">" + SelectLevel + "</td>" +
                    "         </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Câu hỏi:</td>" +
                    "              <td class=\"Col2\"><textarea class=cssTextBoxQuestion id=txtQuestion>" + QuestionCls.Title + "</textarea></td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Điểm:</td>" +
                    "              <td class=\"Col2\"><input id=txtPoint value=\"" + QuestionCls.MaxPoint + "\"></td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Thời gian(giây):</td>" +
                    "              <td class=\"Col2\"><input id=txtSecondTime value=\"" + QuestionCls.SecondTime + "\"></td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Đáp án:</td>" +
                    "              <td class=\"Col2\">" + SelectAnswerText + "</td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td>Câu hỏi trí nhớ:</td>" +
                    "              <td><input type=checkbox id=chkIsMemory " + (QuestionCls.IsMemory == 1 ? "CHECKED" : "") + " ></td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Thời gian ẩn:</td>" +
                    "              <td class=\"Col2\"><input id=txtSecondHidden value=\"" + QuestionCls.SecondHidden + "\"></td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td></td>" +
                    "              <td><input type=button  onclick=javascript:ClientSaveUpdateYesNo('" + QuestionId + "'); value=\"Ghi lại\"><input type=button  onclick=\"javascript:ClientAddNewYesNoQuestion('" + QuestionCls.GroupQuestionId + "','" + QuestionId + "');\" value=\"Câu hỏi mới\"><input type=button  onclick=\"javascript:window.open('" + BackUrl + "','_self');\" value=\"Danh sách\"></td>" +
                    "          </tr>" +
                    "      </table>" +
                    "</div>\r\n";

                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideUpdateYesNo(string SiteId, string QuestionId, string Title, int CorrectOrder, string MaxPoint, string SecondTime, bool IsMemory, string SecondHidden, string SelectLevel)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 

                if (string.IsNullOrEmpty(Title))
                {
                    throw new Exception("Chưa nhập tiêu đề");
                }
                if (FunctionUtilities.checkInteger(MaxPoint) == false)
                {
                    throw new Exception("Dữ liệu điểm nhập không hợp lệ! Phải nhập kiểu số!");
                }
                new QuestionBll().UpdateQuestionYesNo(null, SiteId, QuestionId, Title,CorrectOrder,int.Parse(MaxPoint),int.Parse(SecondTime),IsMemory,int.Parse(SecondHidden),int.Parse(SelectLevel));
                return "Cập nhật thành công!";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}